package com.dingpeikeji.quickdeer.modules.rentproduct.service;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;

import com.dingpeikeji.quickdeer.core.modules.orders.entity.TOrders;
import com.dingpeikeji.quickdeer.core.modules.site.entity.TSitedtl;
import com.dingpeikeji.quickdeer.core.modules.sys.entity.Office;
import com.dingpeikeji.quickdeer.core.modules.sys.entity.User;
import com.dingpeikeji.quickdeer.modules.base.dao.FOfficeDao;
import com.dingpeikeji.quickdeer.modules.base.web.FSysController;
import com.dingpeikeji.quickdeer.modules.rentproduct.dao.FRentproductDao;
import com.dingpeikeji.quickdeer.modules.rentproduct.dao.TCargotypeForsaleDao;
import com.dingpeikeji.quickdeer.modules.rentproduct.entity.TCargotype;
import com.dingpeikeji.quickdeer.modules.rentproduct.entity.TCargotypeForsale;
import com.dingpeikeji.quickdeer.modules.rentproduct.entity.TRentproduct;
import com.dingpeikeji.quickdeer.modules.utils.ValidateTool;
import com.dingpeikeji.quickdeer.modules.wx.common.web.OAuth;

@Service
@Transactional(readOnly = true)
public class FRentproductService {

	@Autowired
	private FOfficeDao fOfficeDao;
	@Autowired
	private FRentproductDao fRentproductDao;
	@Autowired
	private TCargotypeForsaleDao cargotypeForsaleDao;
	
	public TRentproduct getSuitrtnprice(TRentproduct rentproduct) {
		return fRentproductDao.getSuitrtnprice(rentproduct);
	}
	
	public List<Office> findAllBusServer() {
		return fOfficeDao.findAllBusServer();
	}
	
	public List<Office> findCityBusServer(String siteid) {
		return fOfficeDao.findCityBusServer(siteid);
	}
	
	public List<TRentproduct> findInCityList(TRentproduct rentproduct){
		return fRentproductDao.findInCityList(rentproduct);
	}
	
	public List<TRentproduct> findBetweenCityList(TRentproduct rentproduct){
		return fRentproductDao.findBetweenCityList(rentproduct);
	}
	
	public List<TRentproduct> findTravelList(TRentproduct rentproduct){
		return fRentproductDao.findTravelList(rentproduct);
	}
	
	public List<TRentproduct> findPlaneList(TRentproduct rentproduct){
		return fRentproductDao.findPlaneList(rentproduct);
	}
	
	public List<TRentproduct> findTrainList(TRentproduct rentproduct){
		return fRentproductDao.findTrainList(rentproduct);
	}
	
	// 日期格式
	private SimpleDateFormat ymdSdf = new SimpleDateFormat("yyyy-MM-dd");
	
	/**
	 * 返回页面中的“上一天”、“下一天”的数据
	 * @param request
	 * @param model
	 * @param ymdFormat
	 * @param currDate
	 * @param fromdate
	 */
	public void changeDate(HttpServletRequest request, Model model, Date fromdate) {
		Date currDate = new Date();
		// 格式化出发日期，前一天、后一天
		
		// 出发日期
		Calendar departdateCal = Calendar.getInstance();
		departdateCal.setTime(fromdate);
		// 星期数组
		String[] weekdays = new String[]{"日","一","二","三","四","五","六"};
		// 出发日期显示效果，如11月24日星期一
		model.addAttribute("departdateWeek", new SimpleDateFormat("MM月dd日").format(fromdate)+"星期"+weekdays[(departdateCal.get(Calendar.DAY_OF_WEEK)-1)]);
		departdateCal.add(Calendar.DAY_OF_YEAR, -1);
		// 前一天日期
		Date preDate = departdateCal.getTime();
		departdateCal.add(Calendar.DAY_OF_YEAR, 2);
		// 后一天日期
		Date nextDate = departdateCal.getTime();
		// 前一天日期字符串
		String preDateStr = preDate.compareTo(currDate)<0?"":ymdSdf.format(preDate);
		// 后一天日期字符串
		String nextDateStr = ymdSdf.format(nextDate);
		// 前一天跳转链接
		String preUrl = "";
		// 后一天跳转链接
		String nextUrl = "";
		// 当前地址
		String currUrl = request.getRequestURI()+"?";
		
		// 访问地址的参数处理
		
		@SuppressWarnings("unchecked")
		Map<String,Object> param = request.getParameterMap();
		if(param!=null) {
			Set<String> keys = param.keySet();
			Iterator<String> it = keys.iterator();
			while(it.hasNext()){
				String key = it.next();
				String[] values = (String[])param.get(key);
				if(values != null) {
					String p = "&"+key+"=";
					if(key.equals("fromdateStr")) {
						preUrl += p + preDateStr;
						nextUrl += p + nextDateStr;
					} else {
						for(int i=0;i<values.length;i++) {
							if(i!=values.length){
								p += values[i];
							} else {
								p += values[i]+",";
							}
						}
						preUrl += p;
						nextUrl += p;
					}
				}
			}
		}
		model.addAttribute("preUrl", preDateStr == ""? "javascript:;":(currUrl+preUrl));
		model.addAttribute("nextUrl", currUrl+nextUrl);
	}
	
	/**
	 * 根据订单类型返回产品页面名称
	 * @param rentType
	 * @return
	 */
	public String getRentTypeName(String rentType) {
		// 页面类型名
		String rentTypeName = null;
		// 按天包车页面
		if(rentType.equals(TOrders.ORDER_TYPE_RENTPRODUCT_DAY)) {
			rentTypeName = "Day";
		}
		// 旅游包车页面
		else if(rentType.equals(TOrders.ORDER_TYPE_RENTPRODUCT_TRAVEL)) {
			rentTypeName = "Travel";
		}
		// 旅游包车页面
		else if(rentType.equals(TOrders.ORDER_TYPE_RENTPRODUCT_AIRPLANE)) {
			rentTypeName = "Airplane";
		}
		// 旅游包车页面
		else if(rentType.equals(TOrders.ORDER_TYPE_RENTPRODUCT_TRAIN)) {
			rentTypeName = "Train";
		}
		return rentTypeName;
	}
	
	/**
	 * 如果是未登录或者是普通用户身份（非企业用户）则只可查看面向公众的整车
	 * @param tRentproduct
	 * @param request
	 */
	public void setCompAgreerateOffice(TRentproduct tRentproduct, HttpServletRequest request, String type) {
		User user = null;
		if(type.equals("wx")) {
			user = (User)request.getSession().getAttribute(OAuth.SESSION_PUBUSER);
		}
		if(type.equals("f")) {
			user = (User)request.getSession().getAttribute(FSysController.FRONT_USER_IN_SESSION);
		}
		if(user==null || user.getOffice()==null || user.getOffice().getId()==null) {
			tRentproduct.setCompAgreerateOfficeId("");
		} else {
			tRentproduct.setCompAgreerateOfficeId(user.getOffice().getId());
		}
	}
	
	/**
	 * 交换出发地和目的地
	 * @param tRentproduct
	 */
	public void switchCity(TRentproduct tRentproduct) {
		if(tRentproduct.getTabType().equals("送机") || tRentproduct.getTabType().equals("送火车")) {
			TSitedtl temp = tRentproduct.getDepartcity();
			tRentproduct.setDepartcity(tRentproduct.getDestcity());
			tRentproduct.setDestcity(temp);
		}
	}

	public double findLowestPrice(TRentproduct tRentproduct) {
		return fRentproductDao.findLowestPrice(tRentproduct);
	}

	public List<TCargotype> getCargotypeList(TCargotype tCargotype) {
		return fRentproductDao.getCargotypeList(tCargotype);
	}

	public List<TCargotype> findCargotypeList(TCargotype tCargotype) {
		return fRentproductDao.findCargotypeList(tCargotype);
	}

	public List<TCargotypeForsale> findAirCargotypeList(TCargotypeForsale cargotypeForsale) {
		return cargotypeForsaleDao.findAirCargotypeList(cargotypeForsale);
	}

	public List<Office> findAirProductByType(TCargotypeForsale tc) {
		return fRentproductDao.findAirProductByType(tc);
	}

	public int findLastRealscheidFromOrder() {
		return fRentproductDao.findLastRealscheidFromOrder();
	}

	public double getFactor(String name, int disLevel) {
		return fRentproductDao.getFactor(name,disLevel);
	}

	public List<TRentproduct> findInCityListWX(TRentproduct tRentproduct) {
		return fRentproductDao.findInCityListWX(tRentproduct);
	}

	public List<TRentproduct> findBetweenCityListWX(TRentproduct tRentproduct) {
		return fRentproductDao.findBetweenCityListWX(tRentproduct);
	}

	public List<TRentproduct> findTravelListWX(TRentproduct tRentproduct) {
		return fRentproductDao.findTravelListWX(tRentproduct);
	}

	public List<TRentproduct> findPlaneListWX(TRentproduct tRentproduct) {
		return fRentproductDao.findPlaneListWX(tRentproduct);
	}

	public List<TRentproduct> findTravelMobileList(TRentproduct tRentproduct) {
		return fRentproductDao.findTravelMobileList(tRentproduct);
	}
}
